package GDoc::Job;

use Moose;

use Amazon::SQS::Simple;

use GDoc::AWS '$AWS_ACCESS_KEY_ID', '$AWS_SECRET_KEY';
use GDoc::Logger;
use YAML::XS;
use Data::Dumper;

has 'class'  => ( is       => 'ro', 
                  required => 1 
                );

has 'queue'  => ( is       => 'ro', 
                  required => 1, 
                  isa      => 'Str' 
                );

has 'data'   => ( is       => 'ro', 
                  required => 1, 
                  isa => 'HashRef' 
                );

has 'logger' => ( is       => 'ro',
                  init_arg => undef,
                  default  => sub { GDoc::Logger->get_logger }
                );

has '_sqs'   => ( is       => 'ro', 
                  init_arg => undef,
                  default  => sub { Amazon::SQS::Simple->new( $AWS_ACCESS_KEY_ID, $AWS_SECRET_KEY ) }
                );

sub submit { 
    my $self = shift;

    my $q = $self->_sqs->GetQueue( $self->queue );

    my $msg = { class => $self->class, data => $self->data };

    eval { 
        $q->SendMessage( YAML::XS::Dump( $msg ) );
    };

    # need some proper exception handling here
    if ( my $err = $@ ) { 
        open my $tfh, '>>', '/tmp/sqs.out';
        print $tfh Dumper( $msg );
        
        die $err;
    }

}

1;
